package com.hcbd.tdwt.ui.htct.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.hcbd.tdwt.R;
import com.github.mikephil.charting.charts.BarChart;

import com.hcbd.tdwt.base.BaseFragment;
import com.hcbd.tdwt.base.BdManager;
import com.hcbd.tdwt.base.BeidouBoxParams;
import com.hcbd.tdwt.base.GlobalParams;
import com.hcbd.tdwt.util.ChartUtil;
import com.hcbd.tdwt.util.DateUtils;
import com.pangu.bdsdk2021.bluetooth.BluetoothManage;
import com.pangu.bdsdk2021.entity.terminalthree.BDSXXInfo;
import com.pangu.bdsdk2021.entity.terminalthree.BDSnrInfo;
import com.hcbd.tdwt.ui.xingyu.activity.SearchBluetoothActivity_;

import org.androidannotations.annotations.AfterViews;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.OnActivityResult;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.MessageFormat;

@SuppressLint("NonConstantResourceId")
@EFragment(R.layout.fragment_satellite_status)
public class SatelliteStatusFragment extends BaseFragment{
    @ViewById
    ImageButton headLeft;
    @ViewById
    TextView title,deviceName,batteryTv,deviceId;
    @ViewById
    ImageView connectPhoneImg,connectPointImg,connectRouteImg,connectTwoPointImg,connectSatelliteImg,conStatusImg;
    @ViewById
    TextView bdCardTv,bdFreTv,bdStatusTv,deviceConTv,bdCardLevelTv;
    @ViewById
    BarChart mBarChart;
    @ViewById
    TextView dateTv,lonTv,latTv,altTv,speedTv,accuracyTv,notConTv;
    @ViewById
    LinearLayout bluetoothLL;
    private ChartUtil chartUtil;
    private final int BLUETOOTH_CONNECT_CODE=15;//蓝牙连接返回
    @AfterViews
    protected void init() {
        headLeft.setVisibility(View.GONE);
        title.setVisibility(View.GONE);
        chartUtil=new ChartUtil(this.getContext(),mBarChart);
        chartUtil.setBottomColor(R.color.blue_100);
        chartUtil.setTopColor(R.color.light_blue);
        chartUtil.setLeftTextColor(R.color.black);
        chartUtil.setxGridColor(R.color.black);
        chartUtil.setxTvColor(R.color.black);
        chartUtil.initChart();
        refreshStatus();
      //  initPosData();
        initMsgRegister();
    }
    @Click
    void connectLL(){
        if(BeidouBoxParams.isBoxConnectNormal){
            showOldProtocolDialog("是否断开蓝牙连接？", (dialog, confirm) -> {
                if (confirm) {
                    BeidouBoxParams.init();
                    BluetoothManage.getInstance().disConnect();
                    BdManager.getInstance().onDisconnect();
                }
                dialog.dismiss();
            });
        }else {
            if (!BluetoothManage.getInstance().isEnable()) {
                toast("请打开蓝牙！");
                return;
            }
            Intent intent = new Intent();
            intent.setClass(getContext(), SearchBluetoothActivity_.class);
            startActivityForResult(intent, BLUETOOTH_CONNECT_CODE);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 1)
    public void eventBus(BDSXXInfo bdsxxInfo){
        refreshStatus();
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 1)
    public void eventBus(String bleContent){
        refreshStatus();
    }
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 2)
    public void eventBus(BDSnrInfo bdSnrInfo){
        int signal=BeidouBoxParams.getBdSignal();
        if(signal>=GlobalParams.sendBeidouMsgSignalEnable){
            connectTwoPointImg.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.connect_green_point_tt));
            connectSatelliteImg.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.connect_satellite_tt));
        }else{
            connectTwoPointImg.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.connect_not_red_point_tt));
            connectSatelliteImg.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.connect_not_satellite_tt));
        }
        chartUtil.updateChartData();
    }
    @OnActivityResult(BLUETOOTH_CONNECT_CODE)
    void BlueToothConnectResult(){
        refreshStatus();
    }
    /**
     * 初始化终端状态
     */
    private void refreshStatus() {
        chartUtil.updateChartData();
        if (BeidouBoxParams.isBoxConnectNormal) {
            batteryTv.setText(MessageFormat.format("电量:{0}%", BeidouBoxParams.kwh));
            bluetoothLL.setVisibility(View.VISIBLE);
            conStatusImg.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.connect_succes_tt));
            deviceName.setText(MessageFormat.format("蓝牙名称：{0}", BeidouBoxParams.blueName));
            deviceId.setText(MessageFormat.format("终端ID：{0}", BeidouBoxParams.userCardNumber));
            notConTv.setVisibility(View.GONE);

            deviceConTv.setVisibility(View.GONE);
            bdCardTv.setText(String.format("卡号：%s", BeidouBoxParams.userCardNumber));
            bdFreTv.setText(MessageFormat.format("频度：{0}秒", BeidouBoxParams.sendFreq));
            bdCardLevelTv.setText(MessageFormat.format("等级：{0}", BeidouBoxParams.cardType));
            if(BeidouBoxParams.sentWaitSec>0){
                bdStatusTv.setText("状态：繁忙");
            }else{
                bdStatusTv.setText("状态：空闲");
            }
            connectPhoneImg.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.connect_phone_tt));
            connectPointImg.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.connect_green_point_tt));
            connectRouteImg.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.connect_route_tt));
            int signal=BeidouBoxParams.getBdSignal();
            if(signal>=GlobalParams.sendBeidouMsgSignalEnable){
                connectTwoPointImg.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.connect_green_point_tt));
                connectSatelliteImg.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.connect_satellite_tt));
            }else{
                connectTwoPointImg.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.connect_not_red_point_tt));
                connectSatelliteImg.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.connect_not_satellite_tt));
            }
        } else {
            bluetoothLL.setVisibility(View.GONE);
            conStatusImg.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.connect_not_tt));
            deviceName.setText("蓝牙名称：--");
            notConTv.setVisibility(View.VISIBLE);
            batteryTv.setText("电量:--");
            deviceConTv.setVisibility(View.VISIBLE);
            bdCardTv.setText(String.format("卡号:%s", "--"));
            bdFreTv.setText("频度:--");
            bdStatusTv.setText("状态:--");
            bdCardLevelTv.setText("等级:--");
            connectPhoneImg.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.connect_not_phone_tt));
            connectPointImg.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.connect_not_red_point_tt));
            connectRouteImg.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.connect_not_route_tt));
            connectTwoPointImg.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.connect_not_red_point_tt));
            connectSatelliteImg.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.connect_not_satellite_tt));
        }
    }
    public void initPosData(){
        if(dateTv==null){
            return;
        }
        dateTv.setText(DateUtils.getSysDate());
        lonTv.setText(BeidouBoxParams.gpsLongitude);
        latTv.setText(BeidouBoxParams.gpsLatitude);
        altTv.setText(MessageFormat.format("{0}米", BeidouBoxParams.gpsAltitude));
        speedTv.setText(MessageFormat.format("{0}米/秒", BeidouBoxParams.speed));
        accuracyTv.setText(MessageFormat.format("{0}米", BeidouBoxParams.accuracy));
    }
    @Override
    public void  onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(hidden){
            //不可见
            EventBus.getDefault().unregister(this);
        }else{
            refreshStatus();

            EventBus.getDefault().register(this);


        }
    }

}
